package com.scm.java.net.sup.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSON;

import org.apache.commons.collections.map.HashedMap;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.scm.entity.base.Dept;
import com.scm.entity.base.Emp;
import com.scm.entity.base.StoDrugSale;
import com.scm.entity.base.StoPerformance;
import com.scm.entity.base.SupDrugIn;
import com.scm.java.net.sup.service.DrugSaleService;
import com.scm.java.net.system.action.BasicAction;

public class DrugSaleAction extends BasicAction implements
		ModelDriven<StoDrugSale>,Preparable{
    private StoDrugSale drugSale;
    private List<Dept> depts=new ArrayList<Dept>();
    private List<Emp> emps=new ArrayList<Emp>();
    private List<SupDrugIn> drugIns=new ArrayList<SupDrugIn>();
    private DrugSaleService drugSaleService;
   
	public String list() throws Exception {
		emps=dao.queryList("emp.getEmp", null);
		paginatedQuery("drugStock.getDrugSales");
		return "list";
	}
    public String toAdd() throws Exception{
        depts=dao.queryList("dept.getDept", null);
        emps=dao.queryList("emp.getEmp", null);
        drugIns=dao.queryList("drugin.getDrugins", null);
        drugSale.setSaleDate(new Date());
    	return "addPage";
    }
    public String add() throws Exception{
    	drugSaleService.add(drugSale,dao);
    	return "succ";
    }
    //达到编辑页面
    public String toEdit() throws Exception{
    	 depts=dao.queryList("dept.getDept", null);
         emps=dao.queryList("emp.getEmp", null);
         drugIns=dao.queryList("drugin.getDrugins", null);
         Map paraMap=new HashedMap();
         paraMap.put("drugSale", drugSale);
         drugSale=(StoDrugSale) dao.queryList("drugStock.getDrugSales", paraMap).get(0);
         System.out.println(drugSale+"+++++++++");
    	return "editPage";
    }
    //编辑销售单
    public String edit() throws Exception{
    	drugSaleService.edit(drugSale,dao);
    	return "succ";
    }
    //删除销售单
    public String del() throws Exception{
    	dao.del("drugStock.deleteSale",drugSale);
    	return "succ";
    }
   
    public void findRemainDrug() throws Exception{
    	SupDrugIn drugIn=new SupDrugIn();
    	drugIn.setInvoiceid(drugSale.getInvoiceid());
    	Map paraMap=new HashMap();
    	paraMap.put("drugIn", drugIn);
    	drugIn=(SupDrugIn) dao.queryList("drugin.getDrugins", paraMap).get(0);
    	PrintWriter out=response.getWriter();
    	out.print(drugIn.getAmount());
    }
	public List<Dept> getDepts() {
		return depts;
	}
	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}
	public List<Emp> getEmps() {
		return emps;
	}
	public void setEmps(List<Emp> emps) {
		this.emps = emps;
	}
	public List<SupDrugIn> getDrugIns() {
		return drugIns;
	}
	public void setDrugIns(List<SupDrugIn> drugIns) {
		this.drugIns = drugIns;
	}
	@Override
	public StoDrugSale getModel() {
		// TODO Auto-generated method stub
		return drugSale;
	}
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		drugSale=new StoDrugSale();
	}
	public StoDrugSale getDrugSale() {
		return drugSale;
	}
	public void setDrugSale(StoDrugSale drugSale) {
		this.drugSale = drugSale;
	}
	public DrugSaleService getDrugSaleService() {
		return drugSaleService;
	}
	public void setDrugSaleService(DrugSaleService drugSaleService) {
		this.drugSaleService = drugSaleService;
	}
}
